#include<iostream>
//#include "gameboard.h"
#include <string>
//#include <Windows.h>
#include "ai.h"



using namespace std;

class parser
{
private:
AIengine test;
Gameboard board;
bool display;
int level;
//******************************************************** parser

bool comand(string input);
bool comment(string input);
bool move(string input);

//******************************************************* engine
void show_table();
void hide_table();
void select_piece_black();
void select_piece_white();
void show_next_pos();
void AI_select(int depth);
void undo_move();
void redo_move();


public:
void displayer();
bool expr(string input);
};


bool parser:: expr(string input)
{
	
	if(comand(input))
	{
		return true;
	}
	if(move(input))
	{
		return true;
	}
	if(comment(input))
	{
		return true;
	}
}

bool parser:: comand(string input)
{
	for(int i=0;i<input.size();i++)//.leangth in java
	{
		if(input[i]==' ')// indexing does not work in java so just use if(input.indexof(" ")>=0) //no loop required
		{	
			return false;
		}
	}
	if(input== "exit"||input== "EXIT")// input.compareto("exit")
	{
		exit(1);// system.exit()
		return true;
	}
	if(input=="display_on"||input=="DISPLAY_ON")
	{
		display=true;
		return true;
	}
	if(input=="display_off"||input=="DISPLAY_OFF")
	{
		display=false;
		return true;
	}
	if(input=="easy"||input=="EASY")/////////////////////////////
	{
		AI_select(1);
		return true;
	}
	if(input=="medium"||input=="MEDIUM")/////////////////////////////////
	{
		AI_select(2);
		return true;
	}
	if(input=="hard"||input=="HARD")/////////////////////////////////////////
	{
		AI_select(3);
		return true;
	}
	if(input=="white"||input=="WHITE")
	{
		select_piece_white();

		
		return true;
	}
	if(input=="black"||input=="BLACK")
	{
		select_piece_black();
		return true;
	}
	if(input=="undo"||input=="UNDO")//////////////////////////////////
	{
		undo_move();
		return true;
	}
	if(input=="redo"||input=="REDO")/////////////////////////////////
	{
		redo_move();
		return true;
	}
	if(input=="show_next_pos"||input=="SHOW_NEXT_POS")
	{
		show_next_pos();
		return true;
	}
	// cout invalid command
	return false;
}
void parser:: displayer()
{
	board.showNextPos();
	board.display();
	display=true;
}

bool parser:: move(string input)
{ 
	int r,i=0;
	char p,c;
	if(input[i]=='m'&&input[++i]=='o'&&input[++i]=='v'&&input[++i]=='e'&&input[++i]==' ')	
	{
		p=input[++i];
		if(!(p == '1' || p == '2' || p == '3' || p == '4' || p == '5' || p == '6' || p == '7' || p == '8'))
			{
				cout << "Sorry thats not a valid row" << endl;
				return false;
			}
		else
		{
			switch(p)
			{
				case '1':
					r=1;
					break;
				case '2':
					r=2;
					break;
				case '3':
					r=3;
					break;
				case '4':
					r=4;
					break;
				case '5':
					r=5;
					break;
				case '6':
					r=6;
					break;
				case '7':
					r=7;
					break;
				case '8':
					r=8;
					break;
			}
			c=input[++i];
			if(!(c=='a' || c=='b' || c=='c' || c=='d' || c=='e' || c=='f' || c=='g' || c=='h'))
			{
				cout << "Sorry thats not a valid column" << endl;
				return false;
			}
			else
			{
				board.move(r,c);
				board.showNextPos();
				if(display)
					board.display();
				cout << board.currentColor() << "'s turn" << endl << endl;
				cout << endl << "AI IS THINKING..." << endl;
				test.difficulty_of_the_AI(level,board);
				//Sleep(100);
				board.move((int)test.row,(int)test.column);
				board.showNextPos();
				if(display)
					board.display();
				cout << board.currentColor() << "'s turn" << endl << endl;
				return true;
			}
		}
	}

	//incorrect cell number 
	return false;
}

bool parser:: comment(string input)
{
	if(input[0]==';')
	{
		return true;
		cout<< input<< "please give us an A";
	}
	return false;
}	

void parser:: hide_table()
{
	board.showDisplay(false);
}

void parser:: select_piece_black()
{

board.setup();
test.get_color('O');

}

void parser:: select_piece_white()
{

board.setup();
test.get_color('@');
board.showNextPos();
test.difficulty_of_the_AI(level,board);
board.move((int)test.row,(int)test.column);
board.showNextPos();
board.display();

}

void parser:: show_next_pos()// working on this
{
	board.display();

}
		
void parser:: AI_select(int depth)
{
	level=depth;
}

void parser:: undo_move()
{
	board.undo();
}

void parser:: redo_move()
{
	board.redo();
}

int main()
{
	string input;
	parser play;
	cout<<"The following commands are acceptable"<<"\n EXIT | DISPLAY_ON | DISPLAY_OFF | EASY | MEDIUM | HARD| BLACK | WHITE | UNDO | REDO | SHOW_NEXT_POS";
	cout<<"\n To make a move enter command \"move columnrow\""<<"\n Example:  move a1";
	cout<<"\n the display for the board is currently on";
	cout<<" \n entering the color you want (BlACK/WHITE) restarts the game. EXIT closes the program";
	cout<<" \n one may change the level of the game at any time"; 

	cout<<"\n\n\n enter the level of the AI";
		cin>>input;
	if(input=="easy"||input=="EASY"||input=="medium"||input=="MEDIUM"||input=="hard"||input=="HARD")
		play.expr(input);


	cout<<"\n\n\n enter the color you want:";
	cin>>input;
	if(input=="white"||input=="WHITE"||input=="black"||input=="BLACK")
		play.expr(input);

	

	cout<<"HAVE FUN!!!!\n";
	play.displayer();
	while(true)
	{
	cin>>input;
	play.expr(input);
	}
	
	return(0);
}
		
